package cn.stu.projectsummit.comment.service.impl;

import cn.stu.projectsummit.comment.mapper.CommentMapper;
import cn.stu.projectsummit.comment.pojo.dto.CommentQuery;
import cn.stu.projectsummit.comment.pojo.entity.Comment;
import cn.stu.projectsummit.comment.pojo.vo.CommentListVO;
import cn.stu.projectsummit.comment.pojo.vo.CommentVO;
import cn.stu.projectsummit.comment.service.CommentService;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
@Slf4j
public class CommentServiceImpl implements CommentService {
    @Autowired
    CommentMapper commentMapper;

    @Override
    public List<CommentVO> selectComment(CommentQuery commentQuery) {
        log.debug("查询评论业务,commentQuery={}", commentQuery);
        List<CommentVO> list = commentMapper.selectComment(commentQuery);
        return list;
    }
    @Override
    public void deleteComment(Long commentId) {
        log.debug("删除评论业务,commentId={}", commentId);
        commentMapper.delete(commentId);
    }

    @Override
    public List<CommentListVO> selectCommentList(Long blogId) {
        log.debug("查询评论列表业务,帖子id:blogId={}", blogId);
        List<CommentListVO> comments = commentMapper.selectByBlogId(blogId);
        return buildCommentTree(comments);
    }

    @Override
    public void saveComment(CommentVO commentVO) {
        log.debug("保存评论业务,commentVO={}", commentVO);
        Comment comment = new Comment();
        BeanUtils.copyProperties(commentVO, comment);
        comment.setCreateTime(new Date());
        commentMapper.insert(comment);
    }

    private List<CommentListVO> buildCommentTree(List<CommentListVO> comments) {
        // 使用Map来存储每个评论的子评论列表
        Map<Long, List<CommentListVO>> commentMap = new HashMap<>();
        // 遍历所有评论，将它们添加到Map中
        for (CommentListVO comment : comments) {
            commentMap.put(comment.getId(), new ArrayList<>());
        }
        // 再次遍历所有评论，建立父子关系
        List<CommentListVO> rootComments = new ArrayList<>();
        for (CommentListVO comment : comments) {
            if (comment.getParentId() == null) {
                // 如果是根评论，则直接添加到根评论列表中
                rootComments.add(comment);
            } else {
                // 如果是子评论，则添加到对应父评论的子评论列表中
                commentMap.get(comment.getParentId()).add(comment);
            }
        }
            // 递归设置子评论
            for (CommentListVO comment : rootComments) {
                setChildren(comment, commentMap);
            }

            return rootComments;
        }


    // 递归设置子评论的方法
    private void setChildren(CommentListVO parent, Map<Long, List<CommentListVO>> commentMap) {
        List<CommentListVO> children = commentMap.get(parent.getId());
        if (children != null && !children.isEmpty()) {
            parent.setChildren(children);
            for (CommentListVO child : children) {
                setChildren(child, commentMap);
            }
        }
    }
}


